package cn.qn.controller.security;

import cn.qn.base.BaseQuery;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.security.RoleDTO;
import cn.qn.dto.security.RoleMenuDTO;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.moudle.security.Menu;
import cn.qn.moudle.security.Role;
import cn.qn.service.security.MenuService;
import cn.qn.service.security.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author Jeet
 * @description 角色菜单权限管理
 * @date 2019/5/28 14:40
 */
@RestController
@RequestMapping("/security")
public class SecurityController {

    private Logger logger = LoggerFactory.getLogger(SecurityController.class);

    @Reference(check = false)
    private RoleService roleService;
    @Reference(check = false)
    private MenuService menuService;

    /**
     * @param
     * @return
     * @description 角色分页
     * @author Jeet
     * @date 16:05 2019/5/28
     */
    @PostMapping("/role/page")
    public ResponseDTO rolePage(@RequestBody BaseQuery query) {
        RoleDTO dto = new RoleDTO();
        return ResponseDTO.success(roleService.page(dto, query.getPageBounds()));
    }

    @RequestMapping(value = "/role/all",method = RequestMethod.GET)
    public ResponseDTO allRoles() {
        return ResponseDTO.success(roleService.findAllRole());
    }

    /**
     * @return cn.qn.dto.ResponseDTO
     * @description 角色新增
     * @author Jeet
     * @date 16:06 2019/5/28
     **/
    @PostMapping("/role/insert")
    public ResponseDTO roleInsert(@RequestBody Role role) {
        if (null == role || StringUtils.isBlank(role.getCode())) {
            return ResponseDTO.error("角色编码不能为空");
        }
        return ResponseDTO.success(roleService.insert(role));
    }

    /**
     * @description 角色修改
     * @author Jeet
     * @date 16:11 2019/5/28
     **/
    @PostMapping("/role/update")
    public ResponseDTO roleUpdate(@RequestBody Role role) {
        Role role1 = roleService.findByRoleCode(role.getCode());
        if (null == role || null == role.getId() || StringUtils.isBlank(role.getCode())) {
            return ResponseDTO.error("未找到相关角色");
        }
        return ResponseDTO.success(roleService.update(role));
    }

    /**
     * @description 通过角色查询菜单
     * @author Jeet
     * @date 16:11 2019/5/28
     **/
    @GetMapping("/role/menu/page/{id}")
    public ResponseDTO roleUpdate(@PathVariable Integer id) {
        Set<Menu> menuPage = roleService.menuPage(id);
        return ResponseDTO.success(menuPage);
    }

    /**
     * @description 保存角色菜单
     * @author Jeet
     * @date 16:11 2019/5/28
     **/
    @PostMapping("/role/menu/add")
    public ResponseDTO menuAdd(@RequestBody RoleMenuDTO request) {
        Integer roleId = request.getRoleId();
        List<Integer> menuIds = request.getMenuIds();
        boolean i = roleService.menuAdd(roleId, menuIds);
        if (i) {
            logger.info("新增角色菜单信息成功:操作者=" + MemberAccountUtils.getCurrentMember().getUsername() + ", 角色ID=" + roleId + ", 菜单=" + menuIds);
            return ResponseDTO.success(i, "保存成功");
        }
        logger.error("新增角色菜单信息失败:操作者=" + MemberAccountUtils.getCurrentMember().getUsername() + ", 角色ID=" + roleId + ", 菜单=" + menuIds);
        return ResponseDTO.error("保存失败,请稍后再试!");
    }

    /**
     * 查询角色CODE是否存在
     */
    @ApiOperation(value="通过角色CODE查询角色是否存在",notes="通过角色CODE查询角色是否存在",produces = MediaType.APPLICATION_JSON_VALUE,response =Boolean.class )
    @RequestMapping(value = "/exist/{code}" ,method = RequestMethod.GET)
    public ResponseDTO rolePage(@PathVariable String code){
        Role role = roleService.findByRoleCode(code);
        if(null != role && role.getId()>0){
            return ResponseDTO.success(true,"该角色编码已存在,请重新输入");
        }
        return ResponseDTO.success(false);
    }

}
